package com.chen.pfm.provider;

import com.chen.pfm.ResourceTable;
import com.chen.pfm.entity.Classify;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class ClassifyProvider extends BaseItemProvider {
    private List<Classify> list;
    private AbilitySlice slice;

    //要渲染的数据集
    public ClassifyProvider(List<Classify> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }


    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        if (list != null && i >= 0 && i < list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    class ViewHolder {
        Image classimage;
        Image delimage;
        Text name;

        public ViewHolder(Component component) {
            //分类的图标
            classimage = (Image) component.findComponentById(ResourceTable.Id_item_classify_image);

            delimage = (Image) component.findComponentById(ResourceTable.Id_item_classify_delete_bt);
            //分类的名字
            name = (Text) component.findComponentById(ResourceTable.Id_item_classify_text);
        }
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ViewHolder holder = null;
        Classify classify = list.get(i);
        if (component == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_template_items_classify, null, false);
            holder = new ViewHolder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = component;
            holder = (ViewHolder) cpt.getTag();
        }
        holder.classimage.setPixelMap(classify.getImage());
        holder.name.setText(classify.getCname());
        //从列表对象中得到渲染的其中一个
        if (classify.getUid() != -1) {
            //设置图标的要另外处理
            holder.delimage.setPixelMap(ResourceTable.Media_icon_delete);
        }else{
            holder.delimage.setPixelMap(ResourceTable.Media_inco_forbid_delete);
        }
        return cpt;
    }
}
